package cn.com.mjsoft.framework.persistence.core.support.transform;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import cn.com.mjsoft.framework.persistence.core.JdbcInstrument;
import cn.com.mjsoft.framework.persistence.core.RowTransform;
import cn.com.mjsoft.framework.persistence.exception.NotSingleColumnCountException;

/**
 * 对单一结果类型的返回值支持
 * 
 * @author mjsoft
 * 
 */
public class SingleObjectRowTransform implements RowTransform
{

    private static final int SINGLE_INDEX = 1;
    private Class<?> clazzType = null;

    public SingleObjectRowTransform()
    {
    }

    public SingleObjectRowTransform( Class<?> clazzType )
    {
        this.clazzType = clazzType;
    }

    public Class<?> getClazzType()
    {
        return clazzType;
    }

    public void setClazzType( Class<?> clazzType )
    {
        this.clazzType = clazzType;
    }

    public Object convertRow( ResultSet rs, int rowNum ) throws SQLException
    {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        if( columnCount != 1 )
        {
            throw new NotSingleColumnCountException( "当前返回的为非单一列数据" );
        }

        return JdbcInstrument.getResultSetColumnValue( rs, SINGLE_INDEX, clazzType );
    }
}
